--[[
@classmod Layer
]]

return pl.class {
	_name = "Layer",
	_init = function (self, z, name, w, h)
		self.name = name or tostring(z)
		self.z = z
		self.w, self.h =w or 512, h or 512
		self.cvs = _lg.newCanvas(self.w, self.h)
		self.cvs:renderTo(function ()
			_lg.clear(0,0,0, 0)
		end)
	end,
	
	clear = function (self)
		self.cvs:renderTo(function ()
			_lg.clear()
		end)
	end,
	
	update = function (self) end,
	
	get_idata = function (self)
		return self.cvs:newImageData()
	end,
	
	paste = function (self, layer)
		assert(layer:is_a(Layer))
		local canvas = layer.cvs
		self.cvs:renderTo(function ()
		local img_data = canvas:newImageData()
		_lg.draw(_lg.newImage(img_data))
		end)
	end,
	
	export = function (self, filename)
		local filename = "layers/" .. filename..".png"
		local data = self.cvs:newImageData()
		data:encode("png", filename)
	end,
	
	import = function (self, filename)
		local filename = filename or "layers/untitled.png"
		if not _lf.getInfo(filename) then return end
		local data = _li.newImageData(filename)
		local img = $LG.newImage(data)
		self.cvs:renderTo(function ()
			$LG.draw(img)
		end)
	end,
	
	draw = function (self)
		$LG.setColor(1, 1, 1, 1)
		$LG.draw(self.cvs)
		$LG.rectangle("line", 0, 0, self.cvs:getDimensions())
	end,
}
